<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: kane <chengjin005@163.com> 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;

/**
 * Class NavController 导航类别管理控制器
 * @package app\admin\controller
 */
class CustomerController extends AdminBaseController
{

	/**
	 * 客户列表
	 * @return [type] [description]
	 */
	public function index()
	{
		$data = $this->request->param();
		//页码
		$now_page = isset($data['page'])?$data['page']:'1';
		//搜索名称
		$keyword = isset($data['keyword'])?$data['keyword']:'';;
		//分页数据
		$result = Db::name('customer')
						->field("cmf_customer.*,cmf_company.company_name")
						->join('cmf_company','cmf_company.id = cmf_customer.company_id','left')
						->where("(cmf_customer.customer_name like '%$keyword%' or cmf_customer.customer_mobile like '%$keyword%')")
						->order("cmf_customer.update_time desc")
						->paginate(10,false,['query'=>['keyword'=>$keyword]]);
		//总条数
		$sum_result_count = Db::name('customer')
						->where("(cmf_customer.customer_name like '%$keyword%' or cmf_customer.customer_mobile like '%$keyword%')")
						->count();
		$this->assign('result',$result->items());
		$this->assign('page',$result->render());
		$this->assign('now_page',$now_page);
		$this->assign('sum_result_count',$sum_result_count);
		$this->assign('keyword',$keyword);
		return $this->fetch();
	}


	/**
	 * 客户查询页面
	 */
	public function find()
	{
		return $this->fetch();
	}


	/**
	 * 客户查询页面结果
	 */
	public function find_result()
	{
		$data = $this->request->param();
		$customer_name = $data['sid'];
		$result = Db::name('customer')->where("customer_name like '%$customer_name%'")->select()->toArray();
		$str = '';
		if(!$result)
		{	
			$result[0]['customer_name'] = '';
			$result[0]['id'] = 0;
		}
		foreach ($result as $k => $v) {
        	$datas[] = "{'keywords':'" . $v['customer_name'] . "','customer_id':'" . $v['id'] . "'}";
        }
        $str = '[' . implode(',', $datas) . ']';
        echo $str;
        die;
	}

	/**
	 * 客户添加页面
	 */
	public function add()
	{
		return $this->fetch();
	}

	/**
	 * 客户添加
	 */
	public function addPost()
	{
		$post = $this->request->param();
		$data = $post['post'];

		//判断客户是否存在
		$r = Db::name('customer')->where(['customer_mobile'=>$data['customer_mobile']])->find();
		if($r)
		{
			$this->success("该客户已存在");
		}

		$data['desc']        = isset($data['desc'])?html_entity_decode(htmlspecialchars_decode($data['desc'])):'';
		$data['add_time']    = time();
		$data['update_time'] = $data['add_time'];
		Db::name('customer')->insert($data);
		$customer_id = Db::name('customer')->getLastInsID();

		//公司项目信息处理
		if(isset($post['all_project']))
		{
			$all_project = $post['all_project'];
			foreach($all_project as $k=>$v)
			{
				Db::name('project_customer')->insert(['project_id'=>$v,'customer_id'=>$customer_id]);
			}	
		}
		
	
		$this->success("添加成功！", url("customer/index"));
	}

	/**
	 * 客户修改页面
	 */
	public function edit()
	{
		$data = $this->request->param();
		$id   = $data['id'];
		$result = Db::name('customer')
						->field("cmf_customer.*,cmf_company.company_name")
						->join('cmf_company','cmf_company.id = cmf_customer.company_id','left')
						->where(['cmf_customer.id'=>$id])
						->find();
		//客户所管项目信息
		$all_project     = Db::name('project_customer')->where(['customer_id'=>$id])->select()->toArray();

		//客户公司all项目信息
		$company_project_result = Db::name('project')->where(['company_id'=>$result['company_id']])->select()->toArray();

		//处理复选框选中
		foreach($company_project_result as $k=>$v)
		{
			$company_project_result[$k]['is_checked'] = 'false;';

			foreach($all_project as $kk=>$vv)
			{
				if($vv['project_id'] == $v['id'])
				{
					$company_project_result[$k]['is_checked'] = 'true';
				}
			}
		}
		$this->assign('company_project_result',$company_project_result);
		$this->assign('result',$result);
		return $this->fetch();
	}

	/**
	 * 客户修改
	 */
	public function editPost()
	{
		$post = $this->request->param();
		$id   = $post['id'];
		$data = $post['post'];
		$data['desc']        = isset($data['desc'])?html_entity_decode(htmlspecialchars_decode($data['desc'])):'';
		$data['update_time'] = time();

		//删除客户公司项目
		Db::name('project_customer')->where(['customer_id'=>$id])->delete();
		//公司项目信息处理
		if(isset($post['all_project']))
		{
			$all_project = $post['all_project'];
			foreach($all_project as $k=>$v)
			{
				Db::name('project_customer')->insert(['project_id'=>$v,'customer_id'=>$id]);
			}
		}
		Db::name('customer')->where(['id'=>$id])->update($data);
		$this->success('修改成功',url("customer/index"));
	}

	/**
	 * 客户详情
	 */
	public function detail()
	{
		$post = $this->request->param();
		$id   = $post['id'];
		$result = Db::name('customer')->where(['id'=>$id])->find();

		//客户所拥有的项目
		$all_project     = Db::name('project_customer')->where(['customer_id'=>$id])->select()->toArray();
		$all_project_ids = [];
		foreach($all_project as $k=>$v)
		{
			$all_project_ids[] = $v['project_id'];
		}
		$all_project_ids = implode(',',$all_project_ids);
		
		$project_result  = Db::name('project')->where(['id'=>['in',$all_project_ids]])->select()->toArray();

		//判断客户是否负责该项目
		foreach($project_result as $k=>$v)
		{
			if($v['customer_id'] == $result['id'])
			{
				$project_result[$k]['is_fuze'] = '是';
			}else{
				$project_result[$k]['is_fuze'] = '否';
			}
		}

		$status = $this->request->param('status');
		$status = isset($status)?$status:1; //1显示 2 隐藏
		$this->assign('status',$status);

		$this->assign('project_result',$project_result);
		$this->assign('result',$result);
		return $this->fetch();
	}

	public function search()
	{
		$mobile = $this->request->param('mobile');
		$r = Db::name('customer')->where(['customer_mobile'=>$mobile])->find();
		if($r)
		{
			ajaxReturn(['status'=>1,'msg'=>'已经存在']);
		}else
		{
			ajaxReturn(['status'=>2,'msg'=>'未存在']);
		}

	}
}